package com.imddysc.spbbatchitemwriter;

import java.util.Arrays;
import java.util.List;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.job.flow.JobExecutionDecider;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableBatchProcessing
public class ItemWriterDemo {
	
	@Autowired
	private JobBuilderFactory jobBuilderFactory;
	
	@Autowired
	private StepBuilderFactory stepBuilderFactory;

	@Autowired
	@Qualifier("consoleItemWriter")
	private ItemWriter<String> consoleItemWriter;
	
	@Bean
	public Job itemWriterDemoJob() {
		return jobBuilderFactory.get("itemWriterDemoJob")
				.start(itemWriterDemoStep())
				.build();
	}

	@Bean
	public Step itemWriterDemoStep() {
		return stepBuilderFactory.get("itemWriterDemoStep")
				.<String,String>chunk(2)
				.reader(getItemReader())
				.writer(consoleItemWriter)
				.build();
	}

	@Bean
	public MyReader getItemReader() {
		List<String> data = Arrays.asList("cat", "dog", "pig", "duck", "chicken", "fish", "cow");
		return new MyReader(data);
	}

}
